<template>
    <div>
        <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
            <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
                     label-width="120px">
                <el-form-item label="用户名" prop="userName">
                    <el-input v-model="dataForm.userName" placeholder="登录帐号"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password" :class="{ 'is-required': !dataForm.id }" v-if="!dataForm.id">
                    <el-input v-model="dataForm.password" type="password" placeholder="密码"></el-input>
                </el-form-item>

                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="dataForm.email" placeholder="邮箱" :disabled="dataForm.id"></el-input>
                </el-form-item>

                <el-form-item label="状态" size="mini" prop="status">
                    <el-radio-group v-model="dataForm.status">
                        <el-radio :label="0">禁用</el-radio>
                        <el-radio :label="1">正常</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
            <el-button @click="visible = false">取消</el-button>
            <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
        </span>
        </el-dialog>

        <el-dialog
            title="修改密码"
            :visible.sync="dialogVisible"
            width="50%"
        >
            <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
                     label-width="80px">
                <el-form-item label="密码" prop="password" :class="{ 'is-required': !dataForm.id }" >
                    <el-input v-model="dataForm.password"  placeholder="密码"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false" size="small">取 消</el-button>
    <el-button type="primary" @click="handleUpdatePass()" size="small">确 定</el-button>
  </span>
        </el-dialog>
    </div>

</template>

<script>
    import { isEmail , isMobile } from '@/utils/validate'

    export default {
        data () {
            var validatePassword = ( rule , value , callback ) => {
                if ( !this.dataForm.id && !/\S/.test ( value ) ) {
                    callback ( new Error ( '密码不能为空' ) )
                } else {
                    callback ()
                }
            }
            var validateComfirmPassword = ( rule , value , callback ) => {
                if ( !this.dataForm.id && !/\S/.test ( value ) ) {
                    callback ( new Error ( '确认密码不能为空' ) )
                } else if ( this.dataForm.password !== value ) {
                    callback ( new Error ( '确认密码与密码输入不一致' ) )
                } else {
                    callback ()
                }
            }
            var validateEmail = ( rule , value , callback ) => {
                if ( !isEmail ( value ) ) {
                    callback ( new Error ( '邮箱格式错误' ) )
                } else {
                    callback ()
                }
            }
            var validateMobile = ( rule , value , callback ) => {
                if ( !isMobile ( value ) ) {
                    callback ( new Error ( '手机号格式错误' ) )
                } else {
                    callback ()
                }
            }
            return {
                visible : false ,
                roleList : [] ,
                dataForm : {
                    id : 0 ,
                    userName : '' ,
                    password : '' ,
                    comfirmPassword : '' ,
                    salt : '' ,
                    email : '' ,
                    mobile : '' ,
                    roleIdList : [] ,
                    status : 1
                } ,
                dataRule : {
                    userName : [
                        { required : true , message : '用户名不能为空' , trigger : 'blur' }
                    ] ,
                    password : [
                        { validator : validatePassword , trigger : 'blur' }
                    ] ,
                    comfirmPassword : [
                        { validator : validateComfirmPassword , trigger : 'blur' }
                    ] ,
                    email : [
                        { required : true , message : '邮箱不能为空' , trigger : 'blur' } ,
                        { validator : validateEmail , trigger : 'blur' }
                    ] ,
                    mobile : [
                        { required : true , message : '手机号不能为空' , trigger : 'blur' } ,
                        { validator : validateMobile , trigger : 'blur' }
                    ]
                },
                dialogVisible:false
            }
        } ,
        methods : {
            init ( id ) {
                this.visible = true
                this.dataForm.id = id || 0

                    this.$nextTick ( () => {
                        this.$refs[ 'dataForm' ].resetFields ()
                    this.$http ( {
                        url : this.$http.adornUrl ( `/qian/user/info/${ this.dataForm.id }` ) ,
                        method : 'get' ,
                        params : this.$http.adornParams ()
                    } ).then ( ( { data } ) => {
                        if ( data && data.code === 200 ) {
                            this.dataForm.userName = data.user.userName
                            this.dataForm.email = data.user.email
                            this.dataForm.status = data.user.status
                        }
                    } )
                })
            } ,
            // 表单提交
            dataFormSubmit () {
                this.$refs[ 'dataForm' ].validate ( ( valid ) => {
                    if ( valid ) {
                        this.$http ( {
                            url : this.$http.adornUrl ( `/qian/user/${ !this.dataForm.id?'save':'update' }` ) ,
                            method : 'post' ,
                            data : this.$http.adornData ( {
                                'id' : this.dataForm.id || undefined ,
                                'userName' : this.dataForm.userName ,
                                'password' : this.dataForm.password ,
                                'email' : this.dataForm.email ,
                                'status' : this.dataForm.status ,

                            } )
                        } ).then ( ( { data } ) => {
                            if ( data && data.code === 200 ) {
                                this.$message ( {
                                    message : '操作成功' ,
                                    type : 'success' ,
                                    duration : 1500 ,
                                    onClose : () => {
                                        this.visible = false
                                        this.$emit ( 'refreshDataList' )
                                    }
                                } )
                            } else {
                                this.$message.error ( data.msg )
                            }
                        } )
                    }
                } )
            },
            //修改密码
            updatePass(id){
                this.dataForm.id = id
                this.dataForm.password = ''
                this.dialogVisible = true
            },
            handleUpdatePass(){
                this.$http ( {
                    url : this.$http.adornUrl ( `/qian/user/updatePass` ) ,
                    method : 'post' ,
                    data : this.$http.adornData ( {
                        'id' : this.dataForm.id || undefined ,
                        'password' : this.dataForm.password
                    } )
                } ).then ( ( { data } ) => {
                    if ( data && data.code === 200 ) {
                        this.$message ( {
                            message : '操作成功' ,
                            type : 'success' ,
                            duration : 1500 ,
                            onClose : () => {
                                this.dialogVisible = false
                                this.$emit ( 'refreshDataList' )
                            }
                        } )
                    } else {
                        this.$message.error ( data.msg )
                    }
                } )
            }
        }
    }
</script>
